# Contributor: Matthias Ahouansou <matthias@ahouansou.cz>
# Maintainer: Matthias Ahouansou <matthias@ahouansou.cz>
pkgname=ombi
pkgver=4.43.5
pkgrel=1
pkgdesc="Friendly media request tool which automatically syncs with your media servers!"
url="https://ombi.io"
arch="x86_64 aarch64 armv7" # blocked by dotnet6-sdk
license="GPL-2.0-only"
depends="aspnetcore6-runtime sqlite-libs"
makedepends="dotnet6-sdk yarn"
subpackages="$pkgname-openrc"
install="$pkgname.pre-install"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/Ombi-app/Ombi/archive/refs/tags/v$pkgver.tar.gz
	ombi.initd
	ombi.confd
	"
builddir="$srcdir/Ombi-$pkgver"
pkgusers="ombi"
pkggroups="ombi"
options="net" # dotnet and yarn dependencies

export DOTNET_CLI_HOME="$builddir"/dotnetclihome

case $CARCH in
	x86_64)	_dotnet_arch="x64";;
	aarch64)	_dotnet_arch="arm64";;
	armv7)	_dotnet_arch="arm";;
	*)	_dotnet_arch="$CARCH";;
esac

_backend="$builddir/src/Ombi"
_frontend="$_backend/ClientApp"
_artifacts="$_backend/bin/Release/net6.0"
_runtime="linux-musl-$_dotnet_arch"

build() {
	# set backend version
	dotnet new tool-manifest
	dotnet tool install --local dotnet-setversion
	dotnet setversion -r "$pkgver"

	# build the backend
	dotnet publish "$_backend" \
		-c Release \
		-r "$_runtime" \
		--no-self-contained

	# move binaries out of runtime directory
	mv "$_artifacts/$_runtime"/publish/* "$_artifacts" -f
	rm -r "${_artifacts:?}/${_runtime:?}"

	# remove dev config
	rm "$_artifacts"/appsettings.Development.json

	# build frontend
	yarn --cwd "$_frontend" install --network-timeout 120000 --frozen-lockfile
	yarn --cwd "$_frontend" build

	# move frontend to atrifacts directory
	mkdir -p "$_artifacts"/ClientApp
	mv "$_frontend"/dist "$_artifacts"/ClientApp
}

check() {
	make backend-tests
}

package() {
	# setup runtime config file
	mkdir -p "$pkgdir"/var/lib/ombi
	chown -R "$pkgusers:$pkggroups" "$pkgdir"/var/lib/ombi

	local DESTDIR="$pkgdir"/usr/lib/ombi

	mkdir -p "$DESTDIR"
	cp -af "$_artifacts"/* "$DESTDIR"
	chown -R "$pkgusers:$pkggroups" "$DESTDIR"

	install -Dm755 "$srcdir"/ombi.initd "$pkgdir"/etc/init.d/ombi
	install -Dm644 "$srcdir"/ombi.confd "$pkgdir"/etc/conf.d/ombi
}

sha512sums="
92f4eb27d0a1501347014e357738b51d9fe7ef769e744021281da18d74a513bf2c8d4b156a248b3719066bbf1064097a65228084f55d41994bf18abc6d8b05f7  ombi-4.43.5.tar.gz
a48fac800aa0779c793aa1220cce16ccc55dc064487e11daf17b165fbc565e4c62e7a52e1372395508b199a12d6bf773157125f3cbaa845381cc035547b29afa  ombi.initd
7355ae0ffedc179688dc6e4c472bd8963b2cbb851bf00a103febe3138630c1cc8c7af37dc7d2e61bff7dc47214517d5b02bc0e8744894f1ff7272df5f566b8c1  ombi.confd
"
